window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		var curPage = 1;
		var pageSize = 4;
		var totalPage = 0;
		U.fillHead();
		U.fillFooter();
		initPage();

		function initPage() { //初始化页面
			U.loadOpt(false);
			getlist();
		}

		function initEvent() {
			$(".notice_list li").click(function() {
				window.open("announcement_details.html?id=" + $(this).attr("id"));
			});
		}

		function paging_handle(page, method) {
		
			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;
			
			if(lastsize>=totalPage){
				lastsize=totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!")
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕
		
		
		} //分页方法
		function getlist() {
			var params = {
				curPage: curPage,
				pageSize: pageSize,
				visible: 1
			};
			U.loadOpt(true);
			U.ajax("/message/queryByVisi", params, function(json) {
				if(json.success) {
					$(".notice_list>ul").empty();
					$(json.models.messageList).each(function() {
						$(".notice_list>ul").append("<li id='" + this.id + "'><img src='../images/icons/icon_dian.png'><div>" + this.title + "</div><span>" + U.getMyDate(this.gmtCreate) + "</span></li>");
					});
					paging_handle(json.models.page); //分页调用
					U.loadOpt(false);
					initEvent();
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
	});
}